function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val;
  this.left = left === undefined ? null : left;
  this.right = right === undefined ? null : right;
}

const root = new TreeNode(
  1,
  new TreeNode(2, null, new TreeNode(4)),
  new TreeNode(5, null, new TreeNode(7))
);

// 左 -> 中 -> 右
// var inorderTraversal = function (root) {
//   const ans = [];
//   if (!root) return ans;
//   const traversal = (node) => {
//     if (!node) return;
//     traversal(node.left);
//     ans.push(node.val);
//     traversal(node.right);
//   };
//   traversal(root);
//   return ans;
// };

var inorderTraversal = function (root) {
  if (!root) return [];
  if (!root.left && !root.right) return [root.val];
  if (!root.left) return [root.val, ...inorderTraversal(root.right)];
  if (!root.right) return [...inorderTraversal(root.left), root.val];
  return [
    ...inorderTraversal(root.left),
    root.val,
    ...inorderTraversal(root.right),
  ];
};

const ans = inorderTraversal(root);
console.log("ans: ", ans);
